package com.mashiro.system.controller;

import com.mashiro.common.annotation.Log;
import com.mashiro.common.core.controller.BaseController;
import com.mashiro.common.core.domain.AjaxResult;
import com.mashiro.common.enums.BusinessType;
import com.mashiro.common.utils.poi.ExcelUtil;
import com.mashiro.system.domain.MoeTask;
import com.mashiro.system.service.IMoeTaskService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 任务Controller
 *
 * @author mashiro
 * @date 2023-04-18
 */
@RestController
@RequestMapping("/system/task")
public class MoeTaskController extends BaseController {
    @Resource
    private IMoeTaskService moeTaskService;

    /**
     * 查询任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/list")
    public AjaxResult list(MoeTask moeTask) {
        List<MoeTask> list = moeTaskService.selectMoeTaskList(moeTask);
        return success(list);
    }

    /**
     * 导出任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:task:export')")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MoeTask moeTask) {
        List<MoeTask> list = moeTaskService.selectMoeTaskList(moeTask);
        ExcelUtil<MoeTask> util = new ExcelUtil<MoeTask>(MoeTask.class);
        util.exportExcel(response, list, "任务数据");
    }

    /**
     * 获取任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(moeTaskService.selectMoeTaskById(id));
    }

    /**
     * 新增任务
     */
    @PreAuthorize("@ss.hasPermi('system:task:add')")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MoeTask moeTask) {
        return toAjax(moeTaskService.insertMoeTask(moeTask));
    }

    /**
     * 修改任务
     */
    @PreAuthorize("@ss.hasAnyPermi('system:task:edit,system:task:system:task:evaluation')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MoeTask moeTask) {
        return toAjax(moeTaskService.updateMoeTask(moeTask));
    }

    /**
     * 删除任务
     */
    @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        if (moeTaskService.hasChildByTaskId(id)) {
            return warn("存在子菜单,不允许删除");
        }
        return toAjax(moeTaskService.deleteMoeTaskById(id));
    }
}
